package gui;

import org.primefaces.context.RequestContext;

import simulation.SimulationManager;
import utils.CurrentUser;
import utils.Validator;

/**
 * Bean for editing and deleting an exiting simulation and for canceling a
 * running simulation
 * 
 * @author Aleksander Zotov
 * 
 */
public class SimulationBean {

	private String name;
	private String description;

	public String getName() {
		if (ListModel.getSelectedSimForPDF() != null) {
			return ListModel.getSelectedSimForPDF().getName();
		} else
			return null;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		if (ListModel.getSelectedSimForPDF() != null) {
			return ListModel.getSelectedSimForPDF().getDescription();
		} else {
			return null;
		}
	}

	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Changes the name and description of the selected simulation
	 */
	public void changeNameAndDesc() {
		MessageController messageController = new MessageController();
		if (!(name.isEmpty())) {
			if (!(Validator.isValidSimName(name))) {
				messageController
						.showErrorMessage("Name der Berechnung is ungültig!");
			} else {
				ListModel.getSelectedSimForPDF().setName(name);
			}
		} else {
			messageController.showErrorMessage("Name darf nicht leer sein!");
			return;
		}
		if (!(description.isEmpty())) {
			if (!(Validator.isValidSimDescription(description))) {
				messageController
						.showErrorMessage("Beschreibung der Berechnung is ungültig!");
			} else {
				ListModel.getSelectedSimForPDF().setDescription(description);
			}
		} else {
			messageController
					.showErrorMessage("Beschreibung darf nicht leer sein!");
			return;
		}
		RequestContext context = RequestContext.getCurrentInstance();
		context.execute("editSimDialog.hide()");
	}

	/**
	 * Deletes a simulation
	 */
	public void deleteSimulation() {
		SimulationManager simManager = SimulationManager.getInstance();
		MessageController messageController = new MessageController();
		if (CurrentUser.getCurrentUser().getName()
				.equals(ListModel.getSelectedSimForPDF().getUsername())
				|| CurrentUser.getCurrentUser().getName().equals("Admin")) {
			simManager.deleteSimulation(ListModel.getSelectedSimForPDF()
					.getIdentifier());
		} else {
			messageController
					.showErrorMessage("Sie können nur eigene Berechnungen löschen!");
		}
		RequestContext context = RequestContext.getCurrentInstance();
		context.execute("deleteSimDialog.hide()");
	}
}
